package io.mft.as.ar.gateway.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import io.mft.as.ar.gateway.client.BusClient;
import io.mft.as.ar.gateway.client.GateWayManager;
import io.mft.as.ar.gateway.client.impl.BusClientV2;
import io.mft.as.ar.gateway.config.BusConfig;
import io.mft.as.ar.gateway.exception.GatewayClientException;

/**
 * 描述: 
 * 版权: Copyright (c) 2012 
 * 公司: 思迪科技 
 * 作者: 李炜
 * 版本: 1.0 
 * 创建日期: Dec 4, 2013 
 * 创建时间: 2:01:54 PM
 */
public final class BusClientFactory
{
    
    
    /**
     * 
     * 描述：
     * 作者：李炜
     * 时间：Dec 4, 2013 2:04:42 PM
     * @param serverName
     * @return
     */
    public static BusClient getClient(String serverName)
    {
        BusClient busClient = null;
        String version = GateWayManager.getVersion(serverName);
        if ( "2".equals(version) )//调用BUS2.0
        {
            busClient = new BusClientV2();
            busClient.setServerName(serverName);
        }
        else if ( "9".equals(version) )//自定义调用第三方接口
        {
            String classImpl = BusConfig.getString(serverName, "classImpl");
            if ( ObjectUtil.isEmpty(classImpl) )
            {
                throw new GatewayClientException( -1, "自定义调用第三方接口实现类不能为空");
            }
            busClient = (BusClient) ReflectUtil.newInstance(classImpl);
            if ( busClient == null )
            {
                throw new GatewayClientException( -1, "自定义调用第三方接口实现类不存在");
            }
            busClient.setServerName(serverName);
        }
        return busClient;
    }
}
